package com.silverbox.sweeporder.common.util;

import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenAuthTokenAppRequest;
import com.alipay.api.response.AlipayOpenAuthTokenAppResponse;

/**
 * 授权
 * @author Administrator
 *
 */
public class OpenAuthTokenApp {
	/**支付宝网关*/
    public static final String ALIPAY_GATEWAY    = "https://openapi.alipay.com/gateway.do";
    public static final String PRIVATE_KEY       = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANkQr05EI7zEuZbBnA6pbDhGFvpRrfQEypBC/k5QRzA3zvhHaHHOP8BJzrRHxSaAkoUWrs48XS1eogUBlq8TC0GMq8VzvzVJNP0PZWXmegjiSfApaYXRe8VwmtXarpLofsMoTSSj5hHLzFqxxEZoLyVuL8xIryqzqyOKg5zn4a97AgMBAAECgYBhpbnXMZwKDU2X3h0AE4NVhWJM1XW1snoVj62rOT3tGQ4Ibsczex7+t7U/MJImEezX5i5ZJJqeEFY+fkp1E9IZpJS63TkL3TBRiq0fVUlM4NCvGWrWoZbZXodooHFjHbRtbieZdKacQBV438t1OmfRyoMgU3VMu3qHB/k99d2KkQJBAPV60/5exvrnJa+L1KlBlC61gRLTpNCm/5H4eESacXrrkBM+gWff+csiDqH5+47s9uf7ooyRgLpHC+2dpwJT4JcCQQDiXh5MnkN+zqR7Wkm4YOI7LnVbTiwOw3wjBFHzs5OWCwBZfV64qa27FHe+r4WMeNUeU5tFqXrwWXWhg/HFbiC9AkEAhL8XRMcb8zJsPrHvHlhIOboNQpEQ34RvUBB9eRvLoh0gnEeA0xtbiW2xXAZC8tdR7cE19a35XsJswOIwHiIdoQJBAJDF1Ob3iP8+tZ9WfTN4cveanN/KPQ8sIuZaSvdrcAZT/uM89SlxC3VMf9DqfKT8WoDMw89ntwKklEC6HBRCfJkCQC5fsPGJ0ueWMZ5ayjcg8gzmroaACqW2gEtxCTvvyp+/CqGpUZlBgAfDm/h1yR0GQtDMG0cwrsbb9WNzkTI46QQ=";
    public static final String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
    /**字符编码-传递给支付宝的数据编码*/
    public static final String CHARSET           = "GBK";
    
	public static Map<String,String> execute(String appId,String grant_type, String appAuthCode){
		AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_GATEWAY, appId, 
        		PRIVATE_KEY, "json", CHARSET,ALIPAY_PUBLIC_KEY);
	
		AlipayOpenAuthTokenAppRequest authTokenAppRequest = new AlipayOpenAuthTokenAppRequest();
		Map<String, String> map = new HashMap<String, String>();
		try {
			StringBuffer bizContent = new StringBuffer();
			bizContent.append("{");
			bizContent.append("\"grant_type\":\""+grant_type+"\",");
			if("authorization_code".equals(grant_type)){
				bizContent.append("\"code\":\""+appAuthCode+"\"");
			}else if("refresh_token".equals(grant_type)){
				bizContent.append("\"refresh_token\":\""+appAuthCode+"\"");
			}
			bizContent.append("}");
			authTokenAppRequest.setBizContent(bizContent.toString());
			AlipayOpenAuthTokenAppResponse response = alipayClient.execute(authTokenAppRequest);
			
			if (null != response && response.isSuccess()) {
				System.out.println("消息发送成功 : response = " + response.getBody());
				map.put("success", "true");
				map.put("userId", response.getUserId());//授权者的PID
				map.put("alipayAppId", response.getAuthAppId());//授权商户的AppId
				map.put("authToken", response.getAppAuthToken());//商户授权令牌
				map.put("expiresIn", response.getExpiresIn());//令牌有效期
				map.put("refreshToken", response.getAppRefreshToken());//刷新令牌时使用
				map.put("reExpiresIn", response.getReExpiresIn()); //刷新令牌有效期
			}else{
				System.out.println("消息发送失败 code=" + response.getCode() + "  msg=" + response.getSubMsg());
				map.put("success", "false");
				map.put("msg", response.getSubMsg());
			}
		} catch (AlipayApiException e) {
			System.out.println("消息发送失败");
			map.put("success", "false");
			map.put("msg", e.getErrMsg());
		}
		
		return map;
	}
	
}
